package com.biz.controller.basic;

import com.alibaba.fastjson.JSON;
import com.biz.model.Hmodel.Tmebactivity;
import com.biz.model.Pmodel.Pmebactivity;
import com.biz.model.Pmodel.User;
import com.biz.service.basic.MebactivityServiceI;
import com.framework.controller.BaseController;
import com.framework.model.Paging;
import com.framework.model.Params;
import com.framework.utils.SqlFactory;
import com.framework.utils.StringUtil;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 商户设置相关
 * Created by liujiajia on 2016/11/14.
 */
@Controller
@RequestMapping("/mebactivity")
public class MebactivityController extends BaseController{

    @Autowired
    private MebactivityServiceI mebactivityService;


    @InitBinder("pmebactivity")
    public void initBinderFormBean1(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("pmebactivity.");
    }
    /**
     * 跳转活动设置页面
     * @param mv
     * @return
     */
    @RequestMapping("toMebactivity")
    public ModelAndView toMebactivity(ModelAndView mv){
        mv.clear();
        mv.addObject("page_type","1");
        mv.setViewName("basic/mebactivity/mebactivity");
        return mv;
    }
    @RequestMapping("showMebactivity")
    public void showMebactivity(HttpServletResponse response, HttpServletRequest request) throws Exception {
        //必要的分页参数
        SqlFactory factory=new SqlFactory();
        Params sqlParams=factory.getParams();
        sqlParams.setOrder(request.getParameter("order"));
        sqlParams.setPage(Integer.parseInt(request.getParameter("offset")));
        sqlParams.setRows(Integer.parseInt(request.getParameter("limit")));
        sqlParams.setSort(request.getParameter("sort"));
        User user = (User) getShiroAttribute("userinfo");
        sqlParams.setUserId(user.getId());
        sqlParams.setSearchtext(request.getParameter("customer")==null?"":request.getParameter("customer").toString());
        sqlParams.setPage_type(request.getParameter("page_type")==null?"":request.getParameter("page_type").toString());
        Paging paging=mebactivityService.findMebactivityGrid(sqlParams);
        System.out.println(JSON.toJSONString(paging));
        writeJson(paging,response);
    }
    @RequestMapping("delGridById")
    public void delGridById(HttpServletResponse response, HttpServletRequest request,String ids) throws Exception {
        Map<String,Object> map=new HashMap<>();
        try{
            mebactivityService.delGridById(ids);
            map.put("success",true);
        }
        catch (Exception e)
        {
            map.put("success",false);
        }
        writeJson(map,response);
    }

    @RequestMapping("toAdd")
    public ModelAndView toAdd() throws Exception {
        mv.clear();
        mv.setViewName("basic/mebactivity/mebactivity_detail");
        return mv;
    }

    @RequestMapping("toEdit")
    public ModelAndView toEdit(String id) throws Exception {
        mv.clear();
        mv.addObject("id",id);
        mv.setViewName("basic/mebactivity/mebactivity_detail");
        return mv;
    }

    @RequestMapping("loadInfo")
    public void loadInfo(HttpServletResponse response,String id) throws Exception {
        Pmebactivity pmebactivity=mebactivityService.findById(id);
       writeJson(pmebactivity,response);
    }
    @RequestMapping("doSave")
    public void doSave(HttpServletResponse response,Pmebactivity pmebactivity) throws Exception {
     String res="";
        User user = (User) getShiroAttribute("userinfo");
        try{
            if(StringUtil.isNullOrEmpty(pmebactivity.getId()))
//            pmebactivity.setCreateUserId(getShiroAttribute("user"));
            {
                pmebactivity.setCreateUserId(user.getId());
                mebactivityService.saveMebactivity(pmebactivity);}
            else
            {
                pmebactivity.setCreateUserId(user.getId());
                mebactivityService.updateMebactivity(pmebactivity);}
            res="success";
        }
        catch (Exception e)
        {res="操作失败！";}

        writeJson(res,response);
    }

    /**
     * 跳转实体卡报名详情
     * @param mv
     * @return
     */
    @RequestMapping("showDetail")
    public ModelAndView showDetail(ModelAndView mv,String id){
        mv.clear();
        mv.addObject("id",id);
        mv.setViewName("basic/mebactivity/mebactivityPerson");
        return mv;
    }

    /**
     * 子表,码库列表翻页查询
     * @param response
     * @param request
     * @throws Exception
     */
    @RequestMapping("showMebActivityPerson")
    public void showMebActivityPerson(HttpServletResponse response, HttpServletRequest request) throws Exception {
        //必要的分页参数
        Map map = getParameterByRequest(request);

        Paging paging = mebactivityService.showMebActivityPerson(map);

        writeJson(paging, response);
    }

    /**
     * 子表,码库列表翻页查询
     * @param response
     * @param request
     * @throws Exception
     */
    @RequestMapping("changeIsOpen")
    public void changeIsOpen(HttpServletResponse response, HttpServletRequest request,String id,String isOpen) throws Exception {
        Map<String,Object> map=new HashedMap();
        map= mebactivityService.changeIsOpen(id,isOpen);
        writeJson(map, response);
    }

    /**
     * 跳转审批活动页面
     * @param mv
     * @return
     */
    @RequestMapping("toApprove")
    public ModelAndView toApprove(ModelAndView mv){
        mv.clear();
        mv.addObject("page_type","0");
        mv.setViewName("basic/mebactivity/approveActivity");
        return mv;
    }

    @RequestMapping("showApproactivity")
    public void showApproactivity(HttpServletResponse response, HttpServletRequest request) throws Exception {
        //必要的分页参数
        SqlFactory factory=new SqlFactory();
        Params sqlParams=factory.getParams();
        sqlParams.setOrder(request.getParameter("order"));
        sqlParams.setPage(Integer.parseInt(request.getParameter("offset")));
        sqlParams.setRows(Integer.parseInt(request.getParameter("limit")));
        sqlParams.setSort(request.getParameter("sort"));
        User user = (User) getShiroAttribute("userinfo");
        sqlParams.setUserId(user.getId());
        sqlParams.setPage_type(request.getParameter("page_type")==null?"":request.getParameter("page_type").toString());
        sqlParams.setSearchtext(request.getParameter("customer")==null?"":request.getParameter("customer").toString());
        Paging paging=mebactivityService.findMebactivityGrid(sqlParams);
        writeJson(paging,response);
    }

    /**
     * 活动审批
     * @param response
     * @param request
     * @throws Exception
     */
    @RequestMapping("approve")
    public void approve(HttpServletResponse response, HttpServletRequest request,String id,String note,String state) throws Exception {
        Map<String,Object> map=new HashedMap();
        User user = (User) getShiroAttribute("userinfo");
        map= mebactivityService.approve(id,note,state,user.getId());
        writeJson(map, response);
    }


    /**
     * 跳转审批详情页面
     * @param mv
     * @return
     */
    @RequestMapping("showApprove")
    public ModelAndView showApprove(ModelAndView mv,String id){
        mv.clear();
        mv.addObject("id",id);
        mv.setViewName("basic/mebactivity/mebactivityApprove");
        return mv;
    }

    /**
     * 审批详情展示
     * @param response
     * @param request
     * @throws Exception
     */
    @RequestMapping("showMebActivityApprove")
    public void showMebActivityApprove(HttpServletResponse response, HttpServletRequest request) throws Exception {
        //必要的分页参数
        Map map = getParameterByRequest(request);
        Paging paging = mebactivityService.showMebActivityApprove(map);

        writeJson(paging, response);
    }

    /**
     * 跳转报名审批页面
     * @param mv
     * @return
     */
    @RequestMapping("toMebApply")
    public ModelAndView toMebApply(ModelAndView mv){
        mv.clear();
        mv.addObject("page_type","1");
        mv.setViewName("basic/mebactivity/mebApply");
        return mv;
    }

    /**
     * 跳转报名审批详情页面
     * @param mv
     * @return
     */
    @RequestMapping("showApply")
    public ModelAndView showApply(ModelAndView mv,String id){
        mv.clear();
        mv.addObject("id",id);
        mv.setViewName("basic/mebactivity/mebApplyDetail");
        return mv;
    }

    /**
     * 报名详情展示
     * @param response
     * @param request
     * @throws Exception
     */
    @RequestMapping("showApplyDetailList")
    public void showApplyDetailList(HttpServletResponse response, HttpServletRequest request) throws Exception {
        //必要的分页参数
        Map map = getParameterByRequest(request);
        Paging paging = mebactivityService.showApplyDetailList(map);
        writeJson(paging, response);
    }


    /**
     * 报名人审批
     * @param response
     * @param request
     * @throws Exception
     */
    @RequestMapping("approveApply")
    public void approveApply(HttpServletResponse response, HttpServletRequest request,String id,String note,String state) throws Exception {
        Map<String,Object> map=new HashedMap();
        map= mebactivityService.approveApply(id,note,state);
        writeJson(map, response);
    }

    /**
     * 跳转结束活动发积分页面
     * @param mv
     * @return
     */
    @RequestMapping("addInteger")
    public ModelAndView addInteger(ModelAndView mv,String id,String state){
        mv.clear();
        mv.addObject("id",id);
        mv.addObject("state",state);
        mv.setViewName("basic/mebactivity/mebInteger");
        return mv;
    }

    /**
     * 跳转结束活动发积分页面
     * @param mv
     * @return
     */
    @RequestMapping("addReview")
    public ModelAndView addReview(ModelAndView mv,String id){

        Tmebactivity tmebactivity = mebactivityService.getById(id);
        mv.clear();
        mv.addObject("tmebactivity",tmebactivity);
        mv.addObject("id",id);
        mv.setViewName("basic/mebactivity/addReview");
        return mv;
    }

    /**
     * 确认到场页面
     * @param response
     * @param request
     * @throws Exception
     */
    @RequestMapping("showMebIntegerList")
    public void showMebIntegerList(HttpServletResponse response, HttpServletRequest request) throws Exception {
        //必要的分页参数
        Map map = getParameterByRequest(request);
        Paging paging = mebactivityService.showMebIntegerList(map);
        writeJson(paging, response);
    }

    /**
     * 修改状态
     * @param response
     * @param request
     * @throws Exception
     */
    @RequestMapping("updateState")
    public void updateState(HttpServletResponse response, HttpServletRequest request,String id,String state) throws Exception {
        String msg=mebactivityService.updateState(id,state);
        writeJson(msg, response);
    }

    /**
     * 结束活动，发放积分
     * @param response
     * @param request
     * @throws Exception
     */
    @RequestMapping("addMebInteger")
    public void addMebInteger(HttpServletResponse response, HttpServletRequest request,String id) throws Exception {
        String msg=mebactivityService.addMebInteger(id);
        writeJson(msg, response);
    }


    /**
     * 结束活动，发放积分
     * @param response
     * @throws Exception
     */
    @RequestMapping("updateReview")
    public void updateReview(HttpServletResponse response, String id, String container) throws Exception {
        Tmebactivity tmebactivity = mebactivityService.getById(id);

        tmebactivity.setActivityReview(container);
        mebactivityService.update(tmebactivity);

        writeJson(tmebactivity, response);
    }




}
